/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.account.web.controller;

import cn.hutool.core.util.RandomUtil;
import com.medusa.gruul.account.api.model.MemberTopCardInfoDto;
import com.medusa.gruul.account.conf.AccountRedis;
import com.medusa.gruul.account.constant.RedisConstant;
import com.medusa.gruul.account.constant.SmsConstant;
import com.medusa.gruul.account.model.dto.SetMemberDto;
import com.medusa.gruul.account.model.dto.UpdateMemberExpireTimeDto;
import com.medusa.gruul.account.model.dto.UpgradeMemberRankDto;
import com.medusa.gruul.account.model.vo.*;
import com.medusa.gruul.account.service.IMemberInfoService;
import com.medusa.gruul.common.core.constant.CommonConstants;
import com.medusa.gruul.common.core.constant.TimeConstants;
import com.medusa.gruul.common.core.exception.ServiceException;
import com.medusa.gruul.common.core.util.PageUtils;
import com.medusa.gruul.common.core.util.Result;
import com.medusa.gruul.sms.api.dto.SendSmsFeginDto;
import com.medusa.gruul.sms.api.feign.RemoteSmsSendService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.List;

/**
 * @Description: 会员信息功能相关
 * @Author: zhaokw
 * @Date: 2020/5/1 12:58
 **/
@RestController
@RequestMapping("/manager")
@Api(tags = "会员信息相关功能")
public class MemberInfoController {

    @Autowired
    private IMemberInfoService memberInfoService;

    @Autowired
    private RemoteSmsSendService remoteSmsSendService;

    /**
     * 会员信息列表
     *
     * @param page                  size nikeName becomeMemberStartTime becomeMemberEndTime tagId
     * @param orderSuccessStartTime orderSuccessEndTime memberCardId memberNumber sortType
     * @return
     */
    @GetMapping("/memberList")
    @ApiOperation(value = "pc端会员信息列表")
    public Result<PageUtils<List<MemberListVo>>> memberList(
            @ApiParam(value = "指定页数", required = true) @RequestParam Integer page,
            @ApiParam(value = "数据条数", required = true) @RequestParam Integer size,
            @ApiParam(name = "nikeName", value = "微信昵称") @RequestParam(required = false) String nikeName,
            @ApiParam(name = "becomeMemberStartTime", value = "成为会员开始时间") @RequestParam(required = false) String becomeMemberStartTime,
            @ApiParam(name = "becomeMemberEndTime", value = "成为会员结束时间") @RequestParam(required = false) String becomeMemberEndTime,
            @ApiParam(name = "tagId", value = "标签id") @RequestParam(required = false) Long tagId,
            @ApiParam(name = "orderSuccessStartTime", value = "成交时间") @RequestParam(required = false) String orderSuccessStartTime,
            @ApiParam(name = "orderSuccessEndTime", value = "成交时间") @RequestParam(required = false) String orderSuccessEndTime,
            @ApiParam(name = "rankCode", value = "会员等级代码") @RequestParam(required = false) String rankCode,
            @ApiParam(name = "memberCardId", value = "会员卡id") @RequestParam(required = false) Long memberCardId,
            @ApiParam(name = "memberNumber", value = "会员编号") @RequestParam(required = false) String memberNumber,
            @ApiParam(name = "sort", value = "排序类型 1-交易总额降序 2-交易总额升序 3-会员时间降序 4-会员时间升序") @RequestParam(required = false) Integer sortType
    ) {
        PageUtils<List<MemberListVo>> voPageUtils = memberInfoService.memberList(nikeName, becomeMemberStartTime, becomeMemberEndTime,
                tagId, orderSuccessStartTime, orderSuccessEndTime, rankCode, memberCardId, memberNumber, page, size, sortType);
        return Result.ok(voPageUtils);
    }

    /**
     * 发送验证码
     *
     * @param
     * @return
     */
    @GetMapping("/sendCode")
    @ApiOperation(value = "发送验证码")
    public Result sendCode(@ApiParam(value = "手机号", required = true) @RequestParam String phone) {
        //发送验证码
        String code = RandomUtil.randomNumbers(6);
        //验证码有效期未过则重新发送
        SendSmsFeginDto sendSmsFeginDto = new SendSmsFeginDto();
        sendSmsFeginDto.setSmsSendTime(Calendar.getInstance().getTimeInMillis());
        sendSmsFeginDto.setSmsSendMobiles(phone);
        sendSmsFeginDto.setSmsSendParam(code);
        sendSmsFeginDto.setSmsType(SmsConstant.SMS_TYPE_ALIYUN);
        sendSmsFeginDto.setSmsSendZone(SmsConstant.ZONE);
        sendSmsFeginDto.setSignId(SmsConstant.SIGN_ID);
        sendSmsFeginDto.setTemplateId(SmsConstant.TEMPLATE_ID);
        sendSmsFeginDto.setProviderId(SmsConstant.PROVIDER_ID);
        sendSmsFeginDto.setUserId(SmsConstant.USER_ID);
        Result order = remoteSmsSendService.createOrder(sendSmsFeginDto);
        if (order.getCode() != CommonConstants.SUCCESS) {
            throw new ServiceException("短信发送失败,".concat(order.getMsg()));
        }
        AccountRedis accountRedis = new AccountRedis();
        String key = RedisConstant.MEMBER_KEY.concat(phone);

        accountRedis.setNxPx(key, code, TimeConstants.FIVE_MINUTES);

        return Result.ok();
    }

    /**
     * 后台将用户设为会员
     *
     * @param
     * @return
     */
    @PostMapping("/setMember")
    @ApiOperation(value = "后台将用户设为会员")
    public Result setMember(@RequestBody @Validated SetMemberDto setMemberDto) {
        //校验验证码
        AccountRedis accountRedis = new AccountRedis();
        String key = RedisConstant.MEMBER_KEY.concat(setMemberDto.getPhone());
        String code = accountRedis.get(key);
        if (setMemberDto.getCode().equals(code)) {
            memberInfoService.setMember(setMemberDto);
            return Result.ok();
        } else {
            return Result.failed(400, "验证码输入有误，请重新输入！");
        }
    }

    /**
     * 获取租户下会员卡等级
     * 城市合伙人和小程序端已确认无用
     *
     * @param
     * @return
     */
    @GetMapping("/getMemberCardRank")
    @Deprecated
    @ApiOperation(value = "后台获取会员卡等级")
    public Result getMemberCardRank() {
        List<MemberCardRankListVo> memberCardInfoList = memberInfoService.getMemberCardRank();
        return Result.ok(memberCardInfoList);
    }

    /**
     * 获取当前使用中的各等级会员卡
     *
     * @param
     * @return
     */
    @GetMapping("/use/memberCardRank")
    @ApiOperation(value = "获取当前使用中的各等级会员卡")
    public Result<List<UseMemberCardRankVo>> getUseMemberCardRank() {
        List<UseMemberCardRankVo> vos = memberInfoService.getUseMemberCardRank();
        return Result.ok(vos);
    }

    /**
     * 后台更新会员的到期日
     *
     * @param
     * @return
     */
    @PostMapping("/updateMemberExpireTime")
    @ApiOperation(value = "后台更新会员的到期日")
    public Result updateMemberExpireTime(@RequestBody @Validated UpdateMemberExpireTimeDto updateMemberExpireTimeDto) {
        memberInfoService.updateMemberExpireTime(updateMemberExpireTimeDto);
        return Result.ok();
    }

    /**
     * 后台升级会员
     *
     * @param
     * @return
     */
    @PostMapping("/upgradeMemberRank")
    @ApiOperation(value = "后台升级会员等级")
    public Result upgradeMemberRank(@RequestBody @Validated UpgradeMemberRankDto upgradeMemberRankDto) {
        memberInfoService.upgradeMemberRank(upgradeMemberRankDto);
        return Result.ok();
    }

    /**
     * pc端升级会员等级界面，暂考虑该用户所持有的会员卡和后台设置的种类一致，不一致则提示开通，跳转到开通界面
     *
     * @param
     * @return
     */
    @GetMapping("/getMemberCardUpgrade")
    @ApiOperation(value = "pc端升级会员界面")
    public Result<MemberCardUpgradeVo> getMemberCardUpgrade(
            @ApiParam(name = "memberId", value = "会员id") @RequestParam(required = false) String memberId
    ) {
        MemberCardUpgradeVo memberCardUpgrade = memberInfoService.getMemberCardUpgrade(memberId);
        return Result.ok(memberCardUpgrade);
    }

    /**
     * pc端会员记录
     *
     * @param
     * @return
     */
    @GetMapping("/getMemberRecord")
    @ApiOperation(value = "pc端会员记录")
    public Result<PageUtils<List<MemberInnerListVo>>> getMemberRecord(
            @ApiParam(value = "指定页数", required = true) @RequestParam Integer page,
            @ApiParam(value = "数据条数", required = true) @RequestParam Integer size,
            @ApiParam(name = "shopUserId", value = "店铺用户id") @RequestParam(required = true) String shopUserId,
            @ApiParam(name = "memberNumber", value = "会员卡编号") @RequestParam(required = false) String memberNumber,
            @ApiParam(name = "title", value = "会员卡名称") @RequestParam(required = false) String title,
            @ApiParam(name = "rankName", value = "会员卡等级名称") @RequestParam(required = false) String rankName,
            @ApiParam(name = "becomeMemberStartTime", value = "成为会员开始时间") @RequestParam(required = false) String becomeMemberStartTime,
            @ApiParam(name = "becomeMemberEndTime", value = "成为会员结束时间") @RequestParam(required = false) String becomeMemberEndTime
    ) {
        PageUtils<List<MemberInnerListVo>> voPageUtils = memberInfoService.getMemberRecord(page, size, shopUserId, memberNumber, title,
                rankName, becomeMemberStartTime, becomeMemberEndTime);
        return Result.ok(voPageUtils);
    }

    @GetMapping("/getTopMemberCardInfo")
    @ApiOperation(value = "测试用")
    public Result getTopMemberCardInfo(String tenantId) {
        MemberTopCardInfoDto memberTopCardInfoDto = memberInfoService.getTopMemberCardInfo(tenantId);
        return Result.ok(memberTopCardInfoDto);
    }

    @GetMapping("/jump/detail")
    @ApiOperation(value = "当前用户余额明细跳转状态")
    public Result<JumpDetailVo> getJumpDetail() {
        JumpDetailVo vo = memberInfoService.getJumpDetail();
        return Result.ok(vo);
    }


}

